package com.wangyanan.common;


import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * Web配置
 *
 * @author 王亚楠
 * @date 2018年04月13日 16:43
 **/
@Configuration
public class MyView extends WebMvcConfigurationSupport {

    @Override
    public void addInterceptors(InterceptorRegistry registry)
     {
        //注册自定义拦截器，添加拦截路径和排除拦截路径
        registry.addInterceptor(new MyInterceptorHandler())
                .addPathPatterns("/**/**")
                .excludePathPatterns("/");
    }

    @Override
    public void addResourceHandlers( ResourceHandlerRegistry registry ) {
        registry.addResourceHandler("/templates/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/templates/");
        registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/static/");
        super.addResourceHandlers(registry);
    }

    /**
     * 设置 映射URL,forward后会根据addResourceHandlers查找ResourceLocations
     **/
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/templates/index.html");
        registry.addViewController("/index").setViewName("forward:/templates/index.html");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
        super.addViewControllers(registry);
    }

}
